<template>
	<view class="setinfo">
		<u-navbar :titleStyle="{fontWeight:'bold'}" title="编辑信息" />
		<view class="setinfo-content">
			<view class="setinfo-card">
				<view class="setinfo-avatar">
					<view class="avatar-box">
						<view class="avatar-card">
							<u-avatar :src="form.avatar" size="90"></u-avatar>
							<button open-type="chooseAvatar" @chooseavatar="chooseavatar" class="form_data cu-btn">
							</button>
						</view>
						<view class="avatar-icon">
							<u-icon name="camera-fill" size="15" color="#FFF"></u-icon>
						</view>
					</view>
				</view>
				<view class="setinfo-name">
					<view class="setinfo-input">
						<input @blur="blurChang" id="nickname" type="nickname" border="none" placeholder="编辑您的昵称..." v-model="form.name" />
					</view>
					<view class="setinfo-icon">
						<u-icon name="edit-pen" color="#999" size="30"></u-icon>
					</view>
				</view>
				<view class="setinfo-tips">
					提示：建议使用微信头像和昵称，便于订单发货和售后沟通。
				</view>
				<!-- 				<view class="setinfo-exitlogin">
					<view class="exitlogin-text">退出登录</view>
					<view class="exitlogin-right">
						<view class="exitlogin-icon">
							<u-icon name="arrow-right" color="#000" size="20"></u-icon>
						</view>
					</view>
				</view> -->
			</view>
		</view>
		<statusbar>
			<view class="confimorder-botem">
				<button class="cu-btn confimorder-button" @click="saveChang">
					<view class="confimorder-pacie">保存</view>
				</button>
			</view>
		</statusbar>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				form:{
					name: '',
					avatar: ''
				}
			};
		},
		onLoad() {
			this.form.name = this.vuex_user_name
			this.form.avatar = this.vuex_user_avatar
		},
		methods:{
			saveChang(){
				this.$network('/api/user/modInfo',this.form).then(res=>{
					this.$u.toast('保存成功')
					this.$u.vuex('use_switch',true)
					setTimeout(()=>{
						uni.navigateBack()
					},800)
				})
			},
			blurChang(e) {
				const thic = this
				uni.createSelectorQuery().in(this)
					.select("#nickname")
					.fields({
						properties: ["value"],
					})
					.exec((res) => {
						const nickName = res?. [0]?.value
						thic.form.name = nickName
						console.log(thic.form.name)
					})
			},
			chooseavatar(e) {
				const thic = this
				let url = e.detail.avatarUrl
				uni.uploadFile({
					url: thic.acieuploa,
					filePath: url,
					name: 'file',
					success: (uploadFileRes) => {
						const obj = JSON.parse(uploadFileRes.data)
						thic.form.avatar = obj.data.key
					}
				});
			},
		}
	}
</script>

<style lang="scss">
	page {
		background: #F5F5F5;
	}

	.avatar-card {
		position: relative;

		.form_data {
			position: absolute;
			background-color: transparent !important;
			margin: 0;
			padding: 0;
			display: flex;
			align-items: center;
			justify-content: center;
			border: none;
			width: 100%;
			height: 100%;
			top: 0;
			left: 0;
			border-radius: 50%;
		}
	}

	.confimorder-botem {
		width: 100%;
		display: flex;
		align-items: center;
		justify-content: center;

		.confimorder-button {
			width: 100%;
			height: 90rpx;
			background: #FFCF4F;
			color: #FFF;
			font-size: 30rpx;
			font-weight: bold;
			padding: 0;
			margin: 0;
			border-radius: 24px;
		}
	}

	.setinfo-content {
		padding: 10px;

		.setinfo-card {
			width: 100%;
			padding: 25px 10px;
			background: #FFF;
			border-radius: 5px;

			.setinfo-exitlogin {
				display: flex;
				align-items: center;
				justify-content: space-between;
				margin-top: 50rpx;
				border-top: 1px dashed #999;
				padding-top: 40rpx;

				.exitlogin-text {
					font-size: 28rpx;
					color: #000;
					font-weight: bold;
				}

			}

			.setinfo-name {
				margin: 40rpx 0 30rpx 0;
				background: #F7F7F7;
				border-radius: 5px;
				padding: 13px 10px;
				position: relative;
				.setinfo-input{
					width: 90%;
				}
				.setinfo-icon{
					position: absolute;
					top: 8px;
					right: 10px;
				}
			}

			.setinfo-tips {
				display: flex;
				align-items: center;

				.setinfo-icon {
					margin-right: 5rpx;
				}

				font-size: 23rpx;
				color: #999;
			}

			.setinfo-avatar {
				display: flex;
				align-items: center;
				justify-content: center;

				.avatar-box {
					position: relative;

					.avatar-icon {
						position: absolute;
						bottom: 4px;
						right: -2px;
						width: 50rpx;
						height: 50rpx;
						border-radius: 50%;
						background: #FFCF4F;
						border: 2px solid #FFF;
						display: flex;
						align-items: center;
						justify-content: center;
					}
				}
			}
		}
	}
</style>
